<template>
  <div class="common-warp wh100 warp-bg rule-warp">
    <div class="noo">
      <a-card title="基本信息">
        <a-form ref="formRef" :model="state.forma" :rules="rules">
          <a-form-item label="规则名称" name="name">
            <a-input v-model:value="state.forma.name" />
          </a-form-item>
          <a-form-item label="是否跨天" name="isCors">
            <a-radio-group
              :options="plainOptions"
              v-model:value="state.forma.isCors"
            />
          </a-form-item>
        </a-form>
      </a-card>

      <a-card title="时间配置" style="margin-top: 12px; margin-bottom: 12px">
        <a-form ref="formRef" :model="state.formb" :rules="rules">
          <a-form-item label="最早签到" name="inStart">
            <a-time-picker
              v-model:value="state.formb.inStart"
              valueFormat="HH:mm:ss"
              style="width: 100%"
            />
          </a-form-item>
          <a-form-item label="签到时间" name="inTime">
            <a-time-picker
              v-model:value="state.formb.inTime"
              valueFormat="HH:mm:ss"
              style="width: 100%"
            />
          </a-form-item>
          <a-form-item label="最晚签到" name="inEnd">
            <a-time-picker
              v-model:value="state.formb.inEnd"
              valueFormat="HH:mm:ss"
              style="width: 100%"
            />
          </a-form-item>
          <a-form-item label="最早签退" name="outStart">
            <a-time-picker
              v-model:value="state.formb.outStart"
              valueFormat="HH:mm:ss"
              style="width: 100%"
            />
          </a-form-item>
          <a-form-item label="最早签退" name="outTime">
            <a-time-picker
              v-model:value="state.formb.outTime"
              valueFormat="HH:mm:ss"
              style="width: 100%"
            />
          </a-form-item>
          <a-form-item label="最晚签退" name="outEnd">
            <a-time-picker
              v-model:value="state.formb.outEnd"
              valueFormat="HH:mm:ss"
              style="width: 100%"
            />
          </a-form-item>
        </a-form>
      </a-card>
    </div>
    <a-button type="primary" block @click="confirm(formRef)">提交</a-button>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { useAttendRuleCreateApi } from "@/api/index";
import { message } from "ant-design-vue";

const router = useRouter();
const formRef = ref();

const plainOptions = [
  { label: "是", value: 1 },
  { label: "否", value: 2 },
];

const rules = {
  name: [{ required: true, message: "请输入分组名称", trigger: "change" }],
};

const useContenta = () => {
  return {
    isCors: 2,
    name: "",
  };
};
const useContentb = () => {
  return {
    outEnd: "18:30:00",
    outStart: "17:30:00",
    outTime: "18:00:00",

    inEnd: "09:30:00",
    inStart: "08:30:00",
    inTime: "09:00:00",
  };
};

const state = reactive({
  forma: useContenta(),
  formb: useContentb(),
});

//提交
const confirm = (formRef: any) => {
  if (!formRef) return;
  formRef.validate().then(async () => {
    await useAttendRuleCreateApi({ ...state.forma, ...state.formb });
    message.success("操作成功", 2);
    setTimeout(() => {
      router.push({ name: "AttentRule" });
    }, 2300);
  });
};

onMounted(() => {
  let ainput = document.querySelectorAll(".rule-warp .ant-picker-input input");
  ainput.forEach((item: any) => {
    item.addEventListener("focus", () => {
      item.setAttribute("readonly", "false");
    });
  });
  document.title = "新增";
});
</script>
<style scoped lang="less">
.noo {
  height: calc(100vh - 56px);
  overflow-y: scroll;

  :deep(.ant-form-item-label) {
    padding: 0 !important;
  }

  :deep(.ant-card-body) {
    padding: 12px !important;
  }

  :deep(.ant-card-head) {
    padding: 12px !important;
  }

  :deep(.ant-form-item) {
    margin-bottom: 12px !important;
  }
}
</style>
